<?php
/**
 +------------------------------------------------------------------------------
 * PublicAction控制器类
 +------------------------------------------------------------------------------
 * @category   view
 * @package  index
 * @subpackage  index
 * @author   朝闻道 <hydata@gmail.com>
 * @date  2010-8-22
 * @time  下午12:52:49
 +------------------------------------------------------------------------------
 */
class PublicAction extends CommonAction{
	/**
	 * @用户登录
	 * @author   朝闻道 <hydata@gmail.com>
	 * @date:  2010-8-22 / time:  下午12:57:38
	 +------------------------------------------------------------------------------
	 */
	function index() {
		//index_用户登录
		$this->display();
	}//end index_用户登录
	
	/**
	   *提交站点
	   *@date 2010-8-23
	   *@time 上午09:09:54
	   */
	function submit() {
		//submit
		$this->assign('plang',$this->plang);
		$act=Input::getVar($_GET['act']);
		$this->assign('act',$act);
		$dao=D("Cat");
		$data=$dao->where("is_show=1")->findAll();
		$data=list_to_tree($data,'id','pid','_son',0);
		$this->assign('cat_list',$data);
		$this->display();
	}//end submit
	
	/**
	   *接受信息提交
	   *@date 2010-8-24
	   *@time 上午10:19:16
	   */
	function chk() {
		//接受信息提交
		$lang=Input::getVar($_POST['lang']);
		$arc=D("Arc");
		$arclang=D("ArcLang");
		if ($_POST['vip']=='1'){//收费用户
			if(empty($_POST['server'])){
				$this->error(L('_err_not_select_server_'));
			}
			$vo=$arc->create();
			$vo['vipreg']=time();
			$vo['is_show']=0;
			$vo['olang']=$_POST['lang'];
			$vo['server']=array2string($_POST['server'],',');
			$id=$arc->add($vo);
			
			$data=array();
			$data=$arclang->create();
			$data['aid']=$id;
			$langid=$arclang->add($data);
			if($langid){
				$user=array("email"=>$vo['email'],'aid'=>$aid,'password'=>$vo['password']);
				self::create_member($user);
				$this->success('_suc_create_ok_');
			}else{
				$this->error(L('_err_sub_table_'));
			}
		}else{//免费用户
			$vo=$arc->create();
			$vo['olang']=$_POST['lang'];
			$id=$arc->add($vo);
			if($id){
				$user=array("email"=>$vo['email'],'aid'=>$aid);
				self::create_member($user);
			}else{
				$this->error(L('_err_main_table_'));
			}
			$data=array();
			$data=$arclang->create();
			$data['aid']=$id;
			$langid=$arclang->add($data);
			if($langid){
				$user=array("email"=>$email,'aid'=>$aid,'password'=>$vo['password']);
				self::create_member($user);
				$this->success(L('_suc_create_ok_'));
			}else{
				$this->error(L('_err_sub_table_'));
			}
		}
	}//end en_free
	
	public function verify() {
		import ('ORG.Util.Image' );
		if (isset ($_REQUEST ['adv'])) {
			Image::showAdvVerify ();
		} else {
			Image::buildImageVerify ();
		}
	}
	
	/**
	   *用户登录处理
	   *@date 2010-9-6
	   *@time 上午10:04:15
	   */
	function login() {
		//用户登录处理
		if(Session::get('verify')!=md5(trim($_POST['verify']))){
			$this->error(L('_err_verify_'));
		}
		$useroremail=trim($_POST['useroremail']);
		$password=md5(trim($_POST['password']));
		$condition=array();
		if(strpos($useroremail,'@')){
			$condition['email']=$useroremail;
		}else{
			$condition['username']=$useroremail;
		}
		$dao=D("Member");
		$info=$dao->where($condition)->find();
		if($info){
			if($info['password']==$password){
				unset($info['password']);
				Session::set ('info',$info);
				$this->assign("jumpUrl",ru('Member/index'));
				$this->success(L("_suc_login_ok_"));
			}else{
				$this->error(L("_err_password_bad_"));
			}
		}else{
			$this->error(L("_err_useroremail_null_"));
		}
		
	}//end login
	
	/**
	   *用户注册
	   *@date 2010-12-7 / @time 下午03:38:42
	   */
	function register() {
		//用户注册
		$this->display();
	}//end register
	
	/**
	   *注册处理
	   *@date 2010-12-7 / @time 下午03:45:25
	   */
	function doreg() {
		//注册处理
		if(Session::get('verify')!=md5(trim($_POST['verify']))){
			$this->error(L('_err_verify_'));
		}
		$username=trim($_POST['username']);
		$email=trim($_POST['email']);
		$password=trim($_POST['password']);
		$repassword=trim($_POST['repassword']);
		if(empty($username)||empty($email)||empty($password)||empty($repassword)){
			$this->error(L("_err_info_bad_"));
		}
		if($password!=$repassword){
			$this->error(L("_err_repassword_bad_"));
		}
		$dao=D("Member");
		$condition=array();
		$condition['username']=$username;
		$condition['email']=$email;
		$condition['_logic']='or';
		$info=array();
		$info=$dao->where($condition)->find();
		if($info){
			$this->error(L("_err_nameoremail_bad_"));
		}else{
			$vo=$dao->create();
			$vo['is_auto']='0';
			$vo['password']=md5($password);
			$id=$dao->add($vo);
			if($id){
				$info=array();
				$info=$dao->where("id=$id")->find();
				unset($info['password']);
				Session::set ('info',$info);
				$this->assign("jumpUrl",U('Member/index'));
				$this->success(L("_suc_register_ok_"));
			}else{
				$this->error(L("_err_register_bad_"));
			}
		}
	}//end doreg
	
	
	/**
	   *写入一个用户
	   *@date 2010-12-7 / @time 下午01:50:59
	   *@param $email,$aid
	   */
	protected function create_member($user=array()) {
		//写入一个用户
		$dao=D("Member");
		$uid=$dao->add($user);
		if($uid){
			return $uid;
		}else return false;
	}//end create_member
	
	/**
	   *用户退出
	   *@date 2010-12-28 / @time 上午11:10:00
	   */
	function logout() {
		//用户退出
		$this->user=array();
		Session::set("info",null);
		$this->assign("jumpUrl",U('Index/index'));
		$this->success(L("_suc_logout_ok_"));
	}//end logout
}// end class